package com.softstao.guoyu.ui.activity.me;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.Apply;
import com.softstao.guoyu.model.me.RechargeApply;
import com.softstao.guoyu.mvp.interactor.me.BalanceInteractor;
import com.softstao.guoyu.mvp.presenter.me.BalancePresenter;
import com.softstao.guoyu.mvp.viewer.me.RechargeApplyListViewer;
import com.softstao.guoyu.ui.adapter.RechargeApplyAdapter;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.EmptyLayout;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.softstao.softstaolibrary.library.widget.MarginDecoration3;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/4.
 */

public class RechargeApplyListActivity extends BaseActivity implements RechargeApplyListViewer,RechargeApplyAdapter.AdapterItemListener{

    @AIPresenter(presenter = BalancePresenter.class, interactor = BalanceInteractor.class)
    BalancePresenter presenter;

    @BindView(R.id.tab_strip)
    MagicIndicator tabStrip;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @BindView(R.id.more)
    TextView more;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private CommonNavigator commonNavigator;
    private FragmentContainerHelper mFragmentContainerHelper;
    private String[] tabs = new String[]{"全部", "待充值", "已充值", "已取消"};
    private String state = "";
    private List<Apply> datas = new ArrayList<>();
    private RechargeApplyAdapter adapter;
    private int position;
    private String reason;

    @Override
    public int _ContentView() {
        return R.layout.activity_recharge_apply_list;
    }

    @Override
    public void initView() {
        initTitle("我的充值申请");
        scrollView.setOnScrollChangedListener(this);
        setPtrFrameLayoutEnable();

        commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return tabs.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.font_gray));
                colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.font_pink));
                colorTransitionPagerTitleView.setText(tabs[index]);
                colorTransitionPagerTitleView.setTextSize(14);
                colorTransitionPagerTitleView.setWidth(getScreenWidth()/4);
                colorTransitionPagerTitleView.setOnClickListener(v -> {
                    //点击事件
                    mFragmentContainerHelper.handlePageSelected(index);
                    switch (index){
                        case 0:
                            state = "";
                            break;
                        case 1:
                            state = "0";
                            break;
                        case 2:
                            state = "1";
                            break;
                        case 3:
                            state = "2";
                            break;
                    }
                    currentPage = 1;
                    findRechargeApply();
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(getResources().getColor(R.color.font_pink));
                return indicator;
            }
        });
        tabStrip.setNavigator(commonNavigator);
        commonNavigator.getChildAt(0).setHorizontalFadingEdgeEnabled(true);
        commonNavigator.getChildAt(0).setFadingEdgeLength(10);
        mFragmentContainerHelper = new FragmentContainerHelper(tabStrip);

        adapter = new RechargeApplyAdapter(datas);
        adapter.setAdapterItemListener(this);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(this));
        listView.addItemDecoration(new MarginDecoration3(this));
    }

    @OnClick(R.id.submit_btn)
    public void onViewClicked() {
        startActivity(new Intent(context,RechargeApplyActivity.class));
    }

    @Override
    protected void onResume() {
        super.onResume();
        findRechargeApply();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        findRechargeApply();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        findRechargeApply();
    }

    @Override
    public void findRechargeApply() {
        loading.setVisibility(View.VISIBLE);
        presenter.getRechargeApply(currentPage, SharePreferenceManager.getInstance().getAgentId(),state);
    }

    @Override
    public void urgeRechagreApply() {
        loading.setVisibility(View.VISIBLE);
        presenter.urge(SharePreferenceManager.getInstance().getAgentId(),datas.get(position).getId());
    }

    @Override
    public void cancelRechargeApply() {
        loading.setVisibility(View.VISIBLE);
        presenter.cancel(SharePreferenceManager.getInstance().getAgentId(),datas.get(position).getId(),reason);
    }

    @Override
    public void getRechargeApplyList(RechargeApply rechargeApply) {
        loading.setVisibility(View.GONE);
        if (rechargeApply!=null){
            if (rechargeApply.getApplyList()!=null && rechargeApply.getApplyList().size()!=0){
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage==1){
                    datas.clear();
                }
                datas.addAll(rechargeApply.getApplyList());
                adapter.notifyDataSetChanged();
            }else {
                if (currentPage==1){
                    datas.clear();
                    adapter.notifyDataSetChanged();
                    emptyLayout.setVisibility(View.VISIBLE);
                }else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void urgeResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("催促成功");
    }

    @Override
    public void cancelResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("取消成功");
    }

    @Override
    public void urge(int position) {
        this.position = position;
        urgeRechagreApply();
    }

    @Override
    public void cancel(int position) {
        this.position = position;
        View view = getLayoutInflater().inflate(R.layout.layout_cancel_reason, (ViewGroup) findViewById(R.id.dialog));
        EditText editText = (EditText)view.findViewById(R.id.reason);
        new AlertDialog.Builder(context)
                .setMessage("取消充值申请")
                .setView(view)
                .setPositiveButton("提交",(dialog, which) -> {
                    if (TextUtils.isEmpty(editText.getText())){
                        LZToast.getInstance(context).showToast("请输入取消原因");
                    }else {
                        reason = editText.getText().toString();
                        dialog.dismiss();
                        cancelRechargeApply();
                    }
                }).create().show();
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
